package gene.android.accountingpro;

import gene.android.accountingpro.finals.EquationNames;

import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class EquationList extends ListActivity implements OnClickListener {
	
	public static final String CLASS_TAG = EquationList.class.getSimpleName();
	
	public static final String SELECTED_EQUATION_ARRAY = "selected equation array";
	
	private Button nextButton, selectAllButton, clearAllButton;
	private List<String> equationNames;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.equationlist);
		
		equationNames = EquationNames.getAllEquationNames();
		
		setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, equationNames));
		getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		nextButton = (Button)findViewById(R.id.equationlistnextbutton);
		nextButton.setOnClickListener( this );
		selectAllButton = (Button)findViewById(R.id.equationlistselectallbutton);
		selectAllButton.setOnClickListener( this );
		clearAllButton = (Button)findViewById(R.id.equationlistclearallbutton);
		clearAllButton.setOnClickListener( this );
		
	}
	
	public List<String> getSelectedEquationNames()
	{
		ArrayList<String> selectedEquationNames = new ArrayList<String>();
		
		for( int i = 0; i < getListView().getCount(); i++ )
		{
			if( getListView().isItemChecked(i) )
			{
				selectedEquationNames.add( equationNames.get(i) );
			}
		}
		return selectedEquationNames;
		
	}
	
	public List<String> printSelectedEquationNames()
	{
		ArrayList<String> selectedEquationNames = new ArrayList<String>();
		SparseBooleanArray sba = getListView().getCheckedItemPositions();
		
		for( int i = 0; i < sba.size(); i++ )
		{
			Log.e(CLASS_TAG, i +" "+sba.get(i));
		}
		return selectedEquationNames;
		
	}

	@Override
	public void onClick(View v)
	{
		
		if( v.getId() == nextButton.getId() )
		{
			ArrayList<String> selectedEquationNames = new ArrayList<String>( getSelectedEquationNames() );
			printSelectedEquationNames();
			Intent intent = new Intent ( EquationList.this, FormGenerator.class );
			intent.putStringArrayListExtra( SELECTED_EQUATION_ARRAY, selectedEquationNames );
			startActivity(intent);
		}
		
		else if( v.getId() == selectAllButton.getId() )
		{
			for( int i =0; i < getListView().getCount(); i++)
			{
				getListView().setItemChecked(i, true);
			}
		}
		
		else if( v.getId() == clearAllButton.getId() )
		{
			for( int i =0; i < getListView().getCount(); i++)
			{
				getListView().setItemChecked(i, false);
			}
		}
		
		
	}

}
